package ee.greaton.siteManager.GUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import ee.greaton.siteManager.DB.SQLiteConnection;
import ee.greaton.siteManager.FTP.FTPmanager;
import ee.greaton.siteManager.model.Site;

public class SelectFolderDialog extends JDialog implements ActionListener,
		TreeSelectionListener {

	private final JPanel contentPanel = new JPanel();
	private JScrollPane scrollPane;
	private JTree tree;
	private DefaultTreeModel model;
	FTPmanager manager;
	Site site;
	private String selectedDir = "";
	private ManageSitesGUI gui;

	/**
	 * Create the dialog.
	 * 
	 * @param selectedSite
	 * @param manageSitesGUI
	 */
	public SelectFolderDialog(ManageSitesGUI gui, Site selectedSite) {
		site = selectedSite;
		this.gui = gui;
		setBounds(100, 100, 222, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		manager = new FTPmanager(selectedSite);

		scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 206, 228);
		contentPanel.add(scrollPane);
		tree = new JTree();
		tree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);
		model = new DefaultTreeModel(manager.getRootDirectoryTree());
		tree.setModel(model);
		tree.addTreeSelectionListener(this);
		scrollPane.setViewportView(tree);

		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);

		JButton okButton = new JButton("OK");
		okButton.setActionCommand("OK");
		okButton.addActionListener(this);
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
		JButton cancelButton = new JButton("Cancel");
		cancelButton.setActionCommand("Cancel");
		cancelButton.addActionListener(this);
		buttonPane.add(cancelButton);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println(selectedDir);
		switch(e.getActionCommand()){
		case "OK":
			SQLiteConnection sqlite = SQLiteConnection.getInstance();
			sqlite.updateInstallFolder(site, selectedDir);
			gui.refresh();
			break;
		}
		
		manager.close();
		this.dispose();
	}

	public void valueChanged(TreeSelectionEvent event) {
		DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) tree
				.getLastSelectedPathComponent();
		if (parentNode != null && parentNode.getChildCount() == 0) {
			TreePath path = event.getPath();
			setSelectedDir(path);
			ArrayList<DefaultMutableTreeNode> nodeList = manager
					.getDirectoryTree(selectedDir, path.getLastPathComponent()
							.toString());
			for (DefaultMutableTreeNode node : nodeList) {
				model.insertNodeInto(node, parentNode, 0);
			}
		}
	}
	
	private void setSelectedDir(TreePath path){
		ArrayList<String> elements = new ArrayList<String>();
		for (int i = 1; i < path.getPath().length; i++) {
			elements.add(path.getPath()[i].toString());
		}
		String[] pathElements = new String[elements.size()];
		pathElements = elements.toArray(pathElements);
		selectedDir = pathElements[0];
		for (int i = 1; i < pathElements.length; i++) {
			selectedDir += "/" + pathElements[i];
		}
	}
}
